import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:test_flutter/util/battery_channel_utils.dart';
import 'package:test_flutter/util/message_channel_utils.dart';
import 'package:test_flutter/util/notice_channel_utils.dart';

///本地方法调用
///1、Flutter与原生的互相调用
///2、Flutter与原生页面跳转以及传值
///应用的Flutter部分通过平台通道(platform channel)将消息发送到其应用程序所在的宿主(iOS或android)应用
///宿主监听平台通道，并接收该消息，然后它会调用该平台的API，并将响应发送回客户端，即应用程序的Flutter部分
///   a：MethodChannel：用于传递方法调用(method invocation)常用来调用native中某个方法
///       name:String类型，代表Channel的名字，也是其唯一标识
///   b：BasicMessageChannel：用于传递字符串和半结构化的信息，这个用的比较少
///       messager：BinaryMessager类型，代表信息使，是小弟的发送和接收的工具
///   c：EventChannel：用于数据流(event streams)的通信，有监听功能，比如电量变化之后的数据推送给Flutter
///       codec：MessageCodec类型或者MethodCodec类型，代表消息的解码器
///
///注意：编写平台代码的时候要切换对应的开发工具上，比如android代码要用as打开，不然无法导包
class NativePage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() => _NativePageState();
}

class _NativePageState extends State<NativePage> {
  String _result = 'Unknown battery level.';

  _updateData(event) {
    print("====updateData:$event");
    setState(() {
      _result = '$event';
    });
  }

  @override
  void initState() {
    super.initState();

    ///1、初始化通道
    BatteryChannel.initChannels();
    MessageChannelUtils.initChannels();
    // NoticeChannel.initChannels(_updateData);
  }

  @override
  Widget build(BuildContext context) {
    // Widget platformView() {
    //   if (defaultTargetPlatform == TargetPlatform.android) {
    //     return AndroidView(
    //       viewType: 'cn.xqw/custom_platform_view',
    //     );
    //   }
    // }

    return Scaffold(
      appBar: AppBar(
        title: Text('混合开发'),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.spaceEvenly,
          children: [
            ElevatedButton(
                onPressed: () {
                  _getBatteryLevel();
                },
                child: Text('Get Battery Level')),
            ElevatedButton(
                onPressed: () {
                  _getMessage();
                },
                child: Text('Get Message')),
            ElevatedButton(
                onPressed: () {
                  print("click button get Notice");
                  // NoticeChannel.cancel();
                },
                child: Text('Get Notice')),
            Text(_result),
            // Container(
            //   width: 300,
            //   height: 300,
            //   child: platformView(),
            // )
          ],
        ),
      ),
    );
  }

  _getMessage() async {
    print("click button get message");
    MessageResponse ret =
    await MessageChannelUtils.getResult(MessageRequest("张三", 10));
    setState(() {
      _result =
      "get android message response: ${ret.message}, code: ${ret.code}";
    });
  }

  _getBatteryLevel() async {
    print("click button get Battery Level");
    String level = await BatteryChannel.getBatteryLevel();
    setState(() {
      _result = level;
    });
  }
}
